clear all
set more off
set mem 10000000
set matsize 10000
version 15

*******************************************************************************************
*** RDROBUST first stage, VD electricity variables: splits on RGGVY treatment intensity ***
*******************************************************************************************

** Set file paths
do "$path_code/paths.do"

** Set graph scheme
cd "$path/code/analyze"
set scheme fb, perm

****************************************************************** 
****************************************************************** 

{
use "$panel/panel_dataset_full.dta", clear

	// Bring in RGGVY district-level administrative data (10th Plan only)
preserve
use "$rggvy/rggvy_district_progress_X_XI_processed.dta", clear
egen dt_group = group(st_code dt_code)
egen temp_group = group(plan implement_type)
unique st_code, by(temp_group) gen(uniq_st)
unique dt_group, by(temp_group) gen(uniq_dt)
unique dpr_code, by(temp_group) gen(uniq_dpr)
unique st_code if plan==10
unique st_code if plan==11
unique dt_group if plan==10
unique dt_group if plan==11
count if award_date<sanction_date
replace award_date = sanction_date if award_date<sanction_date
replace award_date = max(award_date,17553) if plan==11
keep if plan==10
collapse (min) min_award_date=award_date (max) max_award_date=award_date ///
	(sum) award_cost total_released achiev_UDE achiev_ELEC achiev_BPL ///
	, by(st_code dt_code)	
gen med_award_date = round((min_award_date+max_award_date)/2,1)
format %td med_award_date	
tempfile admin
save `admin'
restore	
merge m:1 st_code dt_code using `admin' 

	// RGGVY admin splitter 1: (# villages treated) / (# villages in district)
egen temp1 = count(vi_code) if tot_p>=300, by(st_code dt_code)
egen temp2 = mode(temp1), by(st_code dt_code)
gen RGGVY_share_villages_300 = (achiev_UDE + achiev_ELEC) / temp2
egen temp3 = count(vi_code), by(st_code dt_code)
gen RGGVY_share_villages_all = (achiev_UDE + achiev_ELEC) / temp3
egen temp_tag = tag(st_code dt_code) 
twoway scatter RGGVY_share_villages_300 RGGVY_share_villages_all if temp_tag
tab state if temp_tag & RGGVY_share_villages_300>1.4 & RGGVY_share_villages_all!=. & corr_state==1
gen RGGVY_rule_breaker = RGGVY_share_villages_300>1.4
twoway scatter RGGVY_share_villages_300 RGGVY_share_villages_all if temp_tag & RGGVY_rule_breaker==0
sum RGGVY_share_villages_all if temp_tag & RGGVY_rule_breaker==0, detail
sum RGGVY_share_villages_300 if temp_tag & RGGVY_rule_breaker==0, detail
// split on 60% of villages in district
drop temp*

	// RGGVY admin splitter 2: (# BPL HHs treated) / (# village HHs in district)
egen temp1 = sum(no_hh11) if tot_p>=300, by(st_code dt_code)
egen temp2 = mode(temp1), by(st_code dt_code)
gen RGGVY_share_hh_300 = (achiev_BPL) / temp2
egen temp3 = sum(no_hh11), by(st_code dt_code)
gen RGGVY_share_hh_all = (achiev_BPL) / temp3
egen temp_tag = tag(st_code dt_code) 
twoway scatter RGGVY_share_hh_300 RGGVY_share_hh_all if temp_tag 
twoway scatter RGGVY_share_hh_300 RGGVY_share_hh_all if temp_tag & RGGVY_rule_breaker==0
sum RGGVY_share_hh_all if temp_tag & RGGVY_rule_breaker==0, detail
sum RGGVY_share_hh_300 if temp_tag & RGGVY_rule_breaker==0, detail
// split on 10% of HHs in district
drop temp*

	// RGGVY admin splitter 3: (Rs allocated) / (# villages treated)
egen temp_tag = tag(st_code dt_code) 
gen RGGVY_lakh_per_v = (total_released) /  (achiev_UDE + achiev_ELEC)
sum RGGVY_lakh_per_v if temp_tag & RGGVY_rule_breaker==0, detail
// split on 10 lakh per village
drop temp*

	// Keep villages in RD sample
gen in_fs_sample = vplan4<11 & corr_state==1 & sample==1 & sample_h==1 & sing_h==1 & pop_non_zero==1 
keep if in_fs_sample==1
	
	// Create state FEs (since RD robust doesn't let you pass them through)
drop if st_code==32 // Kerala, only 3 villages
tab st_code, gen(STFE)	
drop STFE1 // to avoid collinearity

	// Create district FEs (since RD robust doesn't let you pass them through)
gen stdtFE = stdt
tab stdtFE state     if inlist(stdtFE,62,63,64,70,91,175,180,181,202,308,309,320,490,493,494,495,504,505,508)	
replace stdtFE = 999 if inlist(stdtFE,62,63,64,70,91,175,180,181,202,308,309,320,490,493,494,495,504,505,508)
	// one catch-all district FE for districts with so few in-sample villages that they break rdrobust
gen stdthFE = stdtFE
replace stdthFE = 999 if st_code==29 // Karnataka is almost completely missing hour of commercial power!
tab stdtFE, gen(DTFE)	
drop DTFE1 // to avoid collinearity
tab stdthFE, gen(DTFEh)	
drop DTFEh1 // to avoid collinearity

	// Create block groups (for clustering)
egen stdtbk = group(stdt bk_code)

	// Create lights-difference variable, to identify crazy outliers
gen lights_diff = abs(lights_max2011_hat - lights_max2001_hat)

	// Rename variable to make them easier to loop over
rename vdp_pwr_d_com_11 vd_pwr_d_com_11	
rename vdp_pwr_h_*_avg_11 vd_pwr_h_*_11

	// Create variables to store regresson results
gen fs_step = .
gen dpr_step = .
gen yvar = ""
gen ifs = ""
gen dpr_if = ""
gen control = ""
gen fe = ""
gen kernel = ""
gen bwmethod = ""
gen vce = ""
gen polynomial_order = .
gen beta_conv = .
gen beta_robust = .
gen se_conv = .
gen se_robust = .
gen pval_conv = .
gen pval_robust = .
gen lci_conv = .
gen uci_conv = .
gen lci_robust = .
gen uci_robust = .
gen bw_lo = .
gen bw_hi = .
gen nobs_orig = .
gen nobs_left = .
gen nobs_right = .
gen ndist = . 
gen ymean = .
gen fdesc = ""
gen dprtag = ""
local folder = "RDROBUST plots fs vdelec DPR"
local h_wide = 200
local row = 0

	// 1.1 Preferred specification
{
local fs_step = 1
local ifs = "in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
local controls = "lights_max1998 lights_max1999 lights_max2000 lights_max2001 lights_max2002 lights_max2003 lights_max2004 lights_max2005" 
local fe = "STFE*"
local kernel = "tri"
local bwmethod = "mserd"
local vce = ""
local poly = 1

foreach dpr in 1 2 3 4 5 6 7 {
	
	if `dpr'==1 {
		local dpr_if = " & RGGVY_rule_breaker==0"
		local dprtag = "rule"
	}
	else if `dpr'==2 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_villages_all>=0.6"
		local dprtag = "hi_vill"
    }
	else if `dpr'==3 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_villages_all<0.6"
		local dprtag = "lo_vill"
    }
	else if `dpr'==4 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_hh_all>=0.1"
		local dprtag = "hi_hh"
    }
	else if `dpr'==5 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_hh_all<0.1"
		local dprtag = "lo_hh"
    }
	else if `dpr'==6 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_lakh_per_v>=10"
		local dprtag = "hi_funds"
    }
	else if `dpr'==7 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_lakh_per_v<10"
		local dprtag = "lo_funds"
    }

	foreach y in d_com d_all h_com d_agr d_dom h_agr h_dom h_all {

		// Define outcome variable
		local yvar = "vd_pwr_`y'_11"
		if "`y'"=="d_agr" {
			local title = "Dummy for Agricultural Power Access, 2011"
			local ytitle = "Share of villages, residuals"
			local ygap = "yscale(titlegap(*-30))"
		}
		if "`y'"=="d_com" {
			local title = "Dummy for Commercial Power Access, 2011"
			local ytitle = "Share of villages, residuals"
			local ygap = ""
		}
		if "`y'"=="d_dom" {
			local title = "Dummy for Domestic Power Access, 2011"
			local ytitle = "Share of villages, residuals"
			local ygap = "yscale(titlegap(*-30))"
		}
		if "`y'"=="d_all" {
			local title = "Dummy for Power Access, All End Uses, 2011"
			local ytitle = "Share of villages, residuals"
			local ygap = ""
		}
		if "`y'"=="h_agr" {
			local title = "Hours of Agricultural Power Access, 2011"
			local ytitle = "Avg hours per day, residuals"
			local ygap = ""
		}
		if "`y'"=="h_com" {
			local title = "Hours of Commercial Power Access, 2011"
			local ytitle = "Avg hours per day, residuals"
			local ygap = ""
		}
		if "`y'"=="h_dom" {
			local title = "Hours of Domestic Power Access, 2011"
			local ytitle = "Avg hours per day, residuals"
			local ygap = ""
		}
		if "`y'"=="h_all" {
			local title = "Hours of Power Access, All End Uses, 2011"
			local ytitle = "Avg hours per day, residuals"
			local ygap = ""
		}
		local fdesc = "Preferred"
		local ftag = "preferred"

		// Run first-stage regresssion
		rdrobust `yvar' tot_p if `ifs' `dpr_if', c(299.5) covs(`controls' `fe') kernel(`kernel') bwselect(`bwmethod') p(`poly') all `vce'

		// Generate in-sample indicator and store bandwidths
		cap drop temp_in_reg
		qui gen temp_in_reg = `ifs' `dpr_if'  & inrange(tot_p,299.5-e(h_l),299.5+e(h_r)) & `yvar'!=.
		local h_l = e(h_l)
		local h_r = e(h_r)

		// Store results
		local row = `row' + 1
		qui replace fs_step = `fs_step' in `row'
		qui replace dpr_step = `dpr' in `row'
		qui replace yvar = "`yvar'" in `row'
		qui replace ifs = "`ifs'" in `row'
		qui replace dpr_if = "`dpr_if'" in `row'
		qui replace control = "`controls'" in `row'
		qui replace fe = "`fe'" in `row'
		qui replace kernel = e(kernel) in `row'
		qui replace bwmethod = e(bwselect) in `row'
		qui replace vce = "`vce'" in `row'
		qui replace polynomial_order = e(p) in `row'
		qui replace beta_conv = e(tau_cl) in `row'
		qui replace beta_robust = e(tau_bc) in `row'
		qui replace se_conv = e(se_tau_cl) in `row'
		qui replace se_robust = e(se_tau_rb) in `row'
		qui replace pval_conv = e(pv_cl) in `row'
		qui replace pval_robust = e(pv_rb) in `row'
		qui replace lci_conv = e(ci_l_cl) in `row'
		qui replace uci_conv = e(ci_r_cl) in `row'
		qui replace lci_robust = e(ci_l_rb) in `row'
		qui replace uci_robust = e(ci_r_rb) in `row'
		qui replace bw_lo = e(h_l) in `row'
		qui replace bw_hi = e(h_r) in `row'
		qui replace nobs_orig = e(N) in `row'
		qui replace nobs_left = e(N_h_l) in `row'
		qui replace nobs_right = e(N_h_r) in `row'
		qui unique stdt if temp_in_reg==1
		qui replace ndist = r(unique) in `row'
		qui sum `yvar' if temp_in_reg==1 & tot_p<299.5
		qui replace ymean = r(mean) in `row'
		qui replace fdesc = "`fdesc'" in `row'
		qui replace dprtag = "`dprtag'" in `row'

		// Residualize `yvar' for regression sample
		qui reg `yvar' `controls' `fe' if temp_in_reg==1
		cap drop y_resid_sample
		predict y_resid_sample, residuals
		
		// Residualize `yvar' for [100,500] bandwidth
		qui reg `yvar' `controls' `fe' if `ifs' `dpr_if' & inrange(tot_p,299.5-`h_wide',299.5+`h_wide')
		cap drop y_resid_full
		predict y_resid_full, residuals
				
		// RD plots for regression sample (variable bandwidth) 
		foreach bins in 10 20 40 {
			if `h_l'<=75 {
				local xlab = "250 275 300 325 350"
			}
			else if inrange(`h_l',76,125) {
				local xlab = "200 250 300 350 400"
			}
			else if inrange(`h_l',126,175) {
				local xlab = "150 200 250 300 350 400 450"
			}
			else {
				local xlab = ""
			}
			rdplot y_resid_sample tot_p if temp_in_reg==1, ///
				c(299.5) p(`poly') nbins(`bins' `bins') kernel(`kernel') h(`h_l' `h_r') ///
				graph_options( ///
				title("`title'", color(black) size(large)) ///
				ytitle("`ytitle'", size(medlarge)) ///
				xtitle("2001 village population", size(medlarge)) ///
				ylabel(,nogrid angle(0) labsize(medlarge)) ///
				xlabel(`xlab', labsize(medlarge)) ///
				graphregion(color(white)) plotregion(fcolor(white)) graphregion(lcolor(white)) ///
				legend(off))
			graph export "$results/`folder'/`yvar'_fs_`ftag'_`dprtag'_inreg_`bins'.pdf", replace
		}

		// RD plots for regression sample (constant bandwidth) 
		foreach bins in 20 40 {
			rdplot y_resid_full tot_p if `ifs' `dpr_if' & inrange(tot_p,299.5-`h_wide',299.5+`h_wide'), ///
				c(299.5) p(`poly') nbins(`bins' `bins') kernel(`kernel') h(`h_wide' `h_wide') ///
				graph_options( ///
				title("`title'", color(black) size(large)) ///
				ytitle("`ytitle'", size(medlarge)) ///
				xtitle("2001 village population", size(medlarge)) ///
				ylabel(,nogrid angle(0) labsize(medlarge)) `ygap' ///
				xlabel(, labsize(medlarge)) ///
				graphregion(color(white)) plotregion(fcolor(white)) graphregion(lcolor(white)) ///
				legend(off))
			graph export "$results/`folder'/`yvar'_fs_`ftag'_`dprtag'_wide_`bins'.pdf", replace
		}			
	}
}
}

	// 1.4 Sensitivity to different bandwidth calculations
{	
local fs_step = 4
local ifs = "in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
local controls = "lights_max1998 lights_max1999 lights_max2000 lights_max2001 lights_max2002 lights_max2003 lights_max2004 lights_max2005" 
local fe = "STFE*"
local kernel = "tri"
local vce = ""
local poly = 1

foreach dpr in 1 2 3 4 5 6 7 {
	
	if `dpr'==1 {
		local dpr_if = " & RGGVY_rule_breaker==0"
		local dprtag = "rule"
	}
	else if `dpr'==2 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_villages_all>=0.6"
		local dprtag = "hi_vill"
    }
	else if `dpr'==3 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_villages_all<0.6"
		local dprtag = "lo_vill"
    }
	else if `dpr'==4 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_hh_all>=0.1"
		local dprtag = "hi_hh"
    }
	else if `dpr'==5 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_hh_all<0.1"
		local dprtag = "lo_hh"
    }
	else if `dpr'==6 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_lakh_per_v>=10"
		local dprtag = "hi_funds"
    }
	else if `dpr'==7 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_lakh_per_v<10"
		local dprtag = "lo_funds"
    }

	foreach y in d_com d_all h_com d_agr d_dom h_agr h_dom h_all {

		foreach reg in 1 2 3 4 5 {

			// Define bandwidth method
			if `reg'==1 {
				local bwmethod = "msetwo"
			}
			if `reg'==2 {
				local bwmethod = "msesum"
			}
			if `reg'==3 {
				local bwmethod = "cerrd"
			}
			if `reg'==4 {
				local bwmethod = "certwo"
			}
			if `reg'==5 {
				local bwmethod = "cersum"
			}
			local fdesc = "BW `bwmethod'"
			local ftag = "BW`bwmethod'"

			// Define outcome variable
			local yvar = "vd_pwr_`y'_11"
			if "`y'"=="d_agr" {
				local title = "2011 Ag Power Access, BW Method `bwmethod'"
				local ytitle = "Share of villages, residuals"
			}
			if "`y'"=="d_com" {
				local title = "2011 Commercial Power Access, BW Method `bwmethod'"
				local ytitle = "Share of villages, residuals"
			}
			if "`y'"=="d_dom" {
				local title = "2011 Domestic Power Access, BW Method `bwmethod'"
				local ytitle = "Share of villages, residuals"
			}
			if "`y'"=="d_all" {
				local title = "2011 All-Use Power Access, BW Method `bwmethod'"
				local ytitle = "Share of villages, residuals"
			}
			if "`y'"=="h_agr" {
				local title = "2011 Ag Power Hours, BW Method `bwmethod'"
				local ytitle = "Avg hours per day, residuals"
			}
			if "`y'"=="h_com" {
				local title = "2011 Commercial Power Hours, BW Method `bwmethod'"
				local ytitle = "Avg hours per day, residuals"
			}
			if "`y'"=="h_dom" {
				local title = "2011 Domestic Power Hours, BW Method `bwmethod'"
				local ytitle = "Avg hours per day, residuals"
			}
			if "`y'"=="h_all" {
				local title = "2011 All-Use Power Hours, BW Method `bwmethod'"
				local ytitle = "Avg hours per day, residuals"
			}

			// Run first-stage regresssion
			rdrobust `yvar' tot_p if `ifs' `dpr_if', c(299.5) covs(`controls' `fe') kernel(`kernel') bwselect(`bwmethod') p(`poly') all `vce'

			// Generate in-sample indicator and store bandwidths
			cap drop temp_in_reg
			qui gen temp_in_reg = `ifs' `dpr_if' & inrange(tot_p,299.5-e(h_l),299.5+e(h_r)) & `yvar'!=.
			local h_l = e(h_l)
			local h_r = e(h_r)
			
			// Store results
			local row = `row' + 1
			qui replace fs_step = `fs_step' in `row'
			qui replace dpr_step = `dpr' in `row'
			qui replace yvar = "`yvar'" in `row'
			qui replace ifs = "`ifs'" in `row'
			qui replace dpr_if = "`dpr_if'" in `row'
			qui replace control = "`controls'" in `row'
			qui replace fe = "`fe'" in `row'
			qui replace kernel = e(kernel) in `row'
			qui replace bwmethod = e(bwselect) in `row'
			qui replace vce = "`vce'" in `row'
			qui replace polynomial_order = e(p) in `row'
			qui replace beta_conv = e(tau_cl) in `row'
			qui replace beta_robust = e(tau_bc) in `row'
			qui replace se_conv = e(se_tau_cl) in `row'
			qui replace se_robust = e(se_tau_rb) in `row'
			qui replace pval_conv = e(pv_cl) in `row'
			qui replace pval_robust = e(pv_rb) in `row'
			qui replace lci_conv = e(ci_l_cl) in `row'
			qui replace uci_conv = e(ci_r_cl) in `row'
			qui replace lci_robust = e(ci_l_rb) in `row'
			qui replace uci_robust = e(ci_r_rb) in `row'
			qui replace bw_lo = e(h_l) in `row'
			qui replace bw_hi = e(h_r) in `row'
			qui replace nobs_orig = e(N) in `row'
			qui replace nobs_left = e(N_h_l) in `row'
			qui replace nobs_right = e(N_h_r) in `row'
			qui unique stdt if temp_in_reg==1
			qui replace ndist = r(unique) in `row'
			qui sum `yvar' if temp_in_reg==1 & tot_p<299.5
			qui replace ymean = r(mean) in `row'
			qui replace fdesc = "`fdesc'" in `row'
			qui replace dprtag = "`dprtag'" in `row'

			// Residualize `yvar' for regression sample
			qui reg `yvar' `controls' `fe' if temp_in_reg==1
			cap drop y_resid_sample
			predict y_resid_sample, residuals
			
			// RD plots for regression sample (variable bandwidth) 
			foreach bins in 10 20 40 {
				if `h_l'<=75 {
					local xlab = "250 275 300 325 350"
				}
				else if inrange(`h_l',76,125) {
					local xlab = "200 250 300 350 400"
				}
				else if inrange(`h_l',126,175) {
					local xlab = "150 200 250 300 350 400 450"
				}
				else {
					local xlab = ""
				}
				rdplot y_resid_sample tot_p if temp_in_reg==1, ///
					c(299.5) p(`poly') nbins(`bins' `bins') kernel(`kernel') h(`h_l' `h_r') ///
					graph_options( ///
					title("`title'", color(black) size(large)) ///
					ytitle("`ytitle'", size(medlarge)) ///
					xtitle("2001 village population", size(medlarge)) ///
					ylabel(,nogrid angle(0) labsize(medlarge)) ///
					xlabel(`xlab', labsize(medlarge)) ///
					graphregion(color(white)) plotregion(fcolor(white)) graphregion(lcolor(white)) ///
					legend(off))
				graph export "$results/`folder'/`yvar'_fs_`ftag'_`dprtag'_inreg_`bins'.pdf", replace
			}
		}		
	}
}
}


	// 1.5 Sensitivity to 2nd-order polynomial and differet kernels
{	
local fs_step = 5
local ifs = "in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
local controls = "lights_max1998 lights_max1999 lights_max2000 lights_max2001 lights_max2002 lights_max2003 lights_max2004 lights_max2005" 
local fe = "STFE*"
local bwmethod = "mserd"
local vce = ""

foreach dpr in 1 2 3 4 5 6 7 {
	
	if `dpr'==1 {
		local dpr_if = " & RGGVY_rule_breaker==0"
		local dprtag = "rule"
	}
	else if `dpr'==2 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_villages_all>=0.6"
		local dprtag = "hi_vill"
    }
	else if `dpr'==3 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_villages_all<0.6"
		local dprtag = "lo_vill"
    }
	else if `dpr'==4 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_hh_all>=0.1"
		local dprtag = "hi_hh"
    }
	else if `dpr'==5 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_share_hh_all<0.1"
		local dprtag = "lo_hh"
    }
	else if `dpr'==6 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_lakh_per_v>=10"
		local dprtag = "hi_funds"
    }
	else if `dpr'==7 {
		local dpr_if = " & RGGVY_rule_breaker==0 & RGGVY_lakh_per_v<10"
		local dprtag = "lo_funds"
    }

	foreach y in d_com d_all h_com d_agr d_dom h_agr h_dom h_all {

		foreach reg in 2 4 {

			// Define kernel and polynomial combo
			if `reg'==1 {
				local kernel = "tri"
				local poly = 2
				local title_tag = "Tri Kernel, Quadratic"
			}
			if `reg'==2 {
				local kernel = "epa"
				local poly = 1
				local title_tag = "Epa Kernel"
			}
			if `reg'==3 {
				local kernel = "epa"
				local poly = 2
				local title_tag = "Epa Kernel, Quadratic"
			}
			if `reg'==4 {
				local kernel = "uni"
				local poly = 1
				local title_tag = "Uni Kernel"
			}
			if `reg'==5 {
				local kernel = "uni"
				local poly = 2
				local title_tag = "Uni Kernel, Quadratic"
			}
			local fdesc = "kernel `kernel', poly `poly'"
			local ftag = "K`kernel'P`poly'"

			// Define outcome variable
			local yvar = "vd_pwr_`y'_11"
			if "`y'"=="d_agr" {
				local title = "2011 Ag Power Access, `title_tag'"
				local ytitle = "Share of villages, residuals"
			}
			if "`y'"=="d_com" {
				local title = "2011 Commercial Power Access, `title_tag'"
				local ytitle = "Share of villages, residuals"
			}
			if "`y'"=="d_dom" {
				local title = "2011 Domestic Power Access, `title_tag'"
				local ytitle = "Share of villages, residuals"
			}
			if "`y'"=="d_all" {
				local title = "2011 All-Use Power Access, `title_tag'"
				local ytitle = "Share of villages, residuals"
			}
			if "`y'"=="h_agr" {
				local title = "2011 Ag Power Hours, `title_tag'"
				local ytitle = "Avg hours per day, residuals"
			}
			if "`y'"=="h_com" {
				local title = "2011 Commercial Power Hours, `title_tag'"
				local ytitle = "Avg hours per day, residuals"
			}
			if "`y'"=="h_dom" {
				local title = "2011 Domestic Power Hours, `title_tag'"
				local ytitle = "Avg hours per day, residuals"
			}
			if "`y'"=="h_all" {
				local title = "2011 All-Use Power Hours, `title_tag'"
				local ytitle = "Avg hours per day, residuals"
			}

			// Run first-stage regresssion
			rdrobust `yvar' tot_p if `ifs' `dpr_if', c(299.5) covs(`controls' `fe') kernel(`kernel') bwselect(`bwmethod') p(`poly') all `vce'

			// Generate in-sample indicator and store bandwidths
			cap drop temp_in_reg
			qui gen temp_in_reg = `ifs' `dpr_if' & inrange(tot_p,299.5-e(h_l),299.5+e(h_r)) & `yvar'!=.
			local h_l = e(h_l)
			local h_r = e(h_r)
			
			// Store results
			local row = `row' + 1
			qui replace fs_step = `fs_step' in `row'
			qui replace dpr_step = `dpr' in `row'
			qui replace yvar = "`yvar'" in `row'
			qui replace ifs = "`ifs'" in `row'
			qui replace dpr_if = "`dpr_if'" in `row'
			qui replace control = "`controls'" in `row'
			qui replace fe = "`fe'" in `row'
			qui replace kernel = e(kernel) in `row'
			qui replace bwmethod = e(bwselect) in `row'
			qui replace vce = "`vce'" in `row'
			qui replace polynomial_order = e(p) in `row'
			qui replace beta_conv = e(tau_cl) in `row'
			qui replace beta_robust = e(tau_bc) in `row'
			qui replace se_conv = e(se_tau_cl) in `row'
			qui replace se_robust = e(se_tau_rb) in `row'
			qui replace pval_conv = e(pv_cl) in `row'
			qui replace pval_robust = e(pv_rb) in `row'
			qui replace lci_conv = e(ci_l_cl) in `row'
			qui replace uci_conv = e(ci_r_cl) in `row'
			qui replace lci_robust = e(ci_l_rb) in `row'
			qui replace uci_robust = e(ci_r_rb) in `row'
			qui replace bw_lo = e(h_l) in `row'
			qui replace bw_hi = e(h_r) in `row'
			qui replace nobs_orig = e(N) in `row'
			qui replace nobs_left = e(N_h_l) in `row'
			qui replace nobs_right = e(N_h_r) in `row'
			qui unique stdt if temp_in_reg==1
			qui replace ndist = r(unique) in `row'
			qui sum `yvar' if temp_in_reg==1 & tot_p<299.5
			qui replace ymean = r(mean) in `row'
			qui replace fdesc = "`fdesc'" in `row'
			qui replace dprtag = "`dprtag'" in `row'

			// Residualize `yvar' for regression sample
			qui reg `yvar' `controls' `fe' if temp_in_reg==1
			cap drop y_resid_sample
			predict y_resid_sample, residuals
			
			// Residualize `yvar' for [100,500] bandwidth
			qui reg `yvar' `controls' `fe' if `ifs' `dpr_if' & inrange(tot_p,299.5-`h_wide',299.5+`h_wide')
			cap drop y_resid_full
			predict y_resid_full, residuals
			
			// RD plots for regression sample (variable bandwidth) 
			foreach bins in 10 20 40 {
				if `h_l'<=75 {
					local xlab = "250 275 300 325 350"
				}
				else if inrange(`h_l',76,125) {
					local xlab = "200 250 300 350 400"
				}
				else if inrange(`h_l',126,175) {
					local xlab = "150 200 250 300 350 400 450"
				}
				else {
					local xlab = ""
				}
				rdplot y_resid_sample tot_p if temp_in_reg==1, ///
					c(299.5) p(`poly') nbins(`bins' `bins') kernel(`kernel') h(`h_l' `h_r') ///
					graph_options( ///
					title("`title'", color(black) size(large)) ///
					ytitle("`ytitle'", size(medlarge)) ///
					xtitle("2001 village population", size(medlarge)) ///
					ylabel(,nogrid angle(0) labsize(medlarge)) ///
					xlabel(`xlab', labsize(medlarge)) ///
					graphregion(color(white)) plotregion(fcolor(white)) graphregion(lcolor(white)) ///
					legend(off))
				graph export "$results/`folder'/`yvar'_fs_`ftag'_`dprtag'_inreg_`bins'.pdf", replace
			}

			// RD plots for regression sample (constant bandwidth) 
			foreach bins in 20 40 {
				rdplot y_resid_full tot_p if `ifs' `dpr_if' & inrange(tot_p,299.5-`h_wide',299.5+`h_wide'), ///
					c(299.5) p(`poly') nbins(`bins' `bins') kernel(`kernel') h(`h_wide' `h_wide') ///
					graph_options( ///
					title("`title'", color(black) size(large)) ///
					ytitle("`ytitle'", size(medlarge)) ///
					xtitle("2001 village population", size(medlarge)) ///
					ylabel(,nogrid angle(0) labsize(medlarge)) ///
					xlabel(, labsize(medlarge)) ///
					graphregion(color(white)) plotregion(fcolor(white)) graphregion(lcolor(white)) ///
					legend(off))
				graph export "$results/`folder'/`yvar'_fs_`ftag'_`dprtag'_wide_`bins'.pdf", replace
			}
		}		
	}
}
}


	// Save results
keep fs_step-dprtag
dropmiss, obs force
compress
save "$results/RDROBUST_fs_vdelec_dpr.dta", replace

}

****************************************************************** 
****************************************************************** 




